package org.jiuling.wecom.base.authentication;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.jiuling.local.tools.exception.LocalToolsException;
import org.jiuling.network.tools.http.request.HttpRequest;
import org.jiuling.network.tools.http.request.enums.RequestMethodEnum;
import org.jiuling.network.tools.http.request.exception.RequestException;
import org.jiuling.network.tools.http.request.factory.HttpRequestFactory;
import org.jiuling.network.tools.http.request.model.HttpRequestParam;
import org.jiuling.wecom.base.Authorization;
import org.jiuling.wecom.base.enums.WeComApiEnum;
import org.jiuling.wecom.config.WeComConfig;

import java.io.IOException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;

/**
 * 网页授权登录
 * @author 紫灵
 * @date 2023年04月20日 10时22分02秒
 */
public class WebAuthorizedLogin {

    /** 企业微信配置 */
    private final WeComConfig weComConfig;
    /** 授权 */
    private final Authorization authorization;

    public WebAuthorizedLogin(WeComConfig weComConfig, Authorization authorization) {
        this.weComConfig = weComConfig;
        this.authorization = authorization;
    }

    /**
     * 构建网页授权链接
     * @param redirectUri 回调地址
     * @return 授权链接
     */
    public String buildAuthorizationLink(String redirectUri){
        return WeComApiEnum.AUTHORIZATION_LINK.getApi() +
                "?appid=" + weComConfig.getCorpId() +
                "&response_type=code" +
                "&scope=snsapi_base" +
                "&agentid=" + weComConfig.getAgentId() +
                // 设置回调地址
                "&redirect_uri=" + redirectUri +
                "#wechat_redirect";
    }

    public void getUserInfo(String code){
        HttpRequestParam httpRequestParam = new HttpRequestParam()
                .add("access_token",authorization.getToken().getAccessToken())
                .add("code",code);
        HttpRequest httpRequest = new HttpRequest();
        try {
            HttpRequestFactory httpRequestFactory = httpRequest.openHttpRequestFactory(WeComApiEnum.GET_USER_INFO.getApi());
            httpRequestFactory.setRequestParam(httpRequestParam);
            httpRequestFactory.setRequestMethod(RequestMethodEnum.GET);
            JSONObject jsonObject = JSON.parseObject(httpRequest.send().getBody());
            System.out.println(jsonObject);
        } catch (RequestException | IOException | LocalToolsException | NoSuchAlgorithmException |
                 KeyManagementException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        WeComConfig weComConfig = WeComConfig.builder()
                .addAgentId("1000003")
                .addCorpId("wwb8f187e1b4973254")
                .addCorpSecret("4Y-oi0dE8pRKUVjtoWI-Z3oOvv-9in2RPkdZ2S-sDA8")
                .build();
        Authorization authorization = new Authorization(weComConfig);
        WebAuthorizedLogin webAuthorizedLogin = new WebAuthorizedLogin(weComConfig,authorization);
        webAuthorizedLogin.getUserInfo("D-0vspYXpBv9QPxd8bbGUVCgoEUARzCMAMJfq69WicA");
    }

}
